package com.ruoyi.blog.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.blog.domain.BlogArticleTag;
import com.ruoyi.blog.service.IBlogArticleTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.blog.mapper.BlogTagsMapper;
import com.ruoyi.blog.domain.BlogTags;
import com.ruoyi.blog.service.IBlogTagsService;

/**
 * 标签Service业务层处理
 * 
 * @author 谭潇
 * @date 2021-05-08
 */
@Service
public class BlogTagsServiceImpl implements IBlogTagsService 
{
    @Autowired
    private BlogTagsMapper blogTagsMapper;

    @Autowired
    private IBlogArticleTagService iBlogArticleTagService;

    /**
     * 查询标签
     * 
     * @param id 标签ID
     * @return 标签
     */
    @Override
    public BlogTags selectBlogTagsById(Long id)
    {
        return blogTagsMapper.selectBlogTagsById(id);
    }

    /**
     * 查询标签列表
     * 
     * @param blogTags 标签
     * @return 标签
     */
    @Override
    public List<BlogTags> selectBlogTagsList(BlogTags blogTags)
    {
        return blogTagsMapper.selectBlogTagsList(blogTags);
    }

    /**
     * 新增标签
     * 
     * @param blogTags 标签
     * @return 结果
     */
    @Override
    public int insertBlogTags(BlogTags blogTags)
    {
        return blogTagsMapper.insertBlogTags(blogTags);
    }

    /**
     * 修改标签
     * 
     * @param blogTags 标签
     * @return 结果
     */
    @Override
    public int updateBlogTags(BlogTags blogTags)
    {
        return blogTagsMapper.updateBlogTags(blogTags);
    }

    /**
     * 批量删除标签
     * 
     * @param ids 需要删除的标签ID
     * @return 结果
     */
    @Override
    public int deleteBlogTagsByIds(Long[] ids)
    {
        return blogTagsMapper.deleteBlogTagsByIds(ids);
    }

    /**
     * 删除标签信息
     * 
     * @param id 标签ID
     * @return 结果
     */
    @Override
    public int deleteBlogTagsById(Long id)
    {
        return blogTagsMapper.deleteBlogTagsById(id);
    }

    @Override
    public BlogTags selectBlogTagsByName(String name) {
        return blogTagsMapper.selectBlogTagsByName(name);
    }

    public String addBlogArticleTags(List<BlogTags> tagsList, Long blogArticleId) {
        StringBuilder stringBuilder = new StringBuilder();
        for (BlogTags tags: tagsList) {
            stringBuilder.append(tags.getName() ).append(",");
            BlogTags selectTag = this.selectBlogTagsByName(tags.getName() );
            long tagId = 0;
            if (selectTag != null){
                tagId = selectTag.getId();
            }else{
                BlogTags insertTags = new BlogTags();
                insertTags.setName(tags.getName() );
                this.insertBlogTags(insertTags);
                tagId = insertTags.getId();
            }
            // 处理关联数据
            BlogArticleTag blogArticleTag = new BlogArticleTag();
            blogArticleTag.setArticleId(blogArticleId);
            blogArticleTag.setTagId(tagId);

            iBlogArticleTagService.insertBlogArticleTag(blogArticleTag);
        }
        return stringBuilder.toString();
    }

    @Override
    public List<BlogTags> selectBlogTagsList() {
        return blogTagsMapper.selectList(new QueryWrapper<>());
    }
}
